package com.yuecai.modules.activity_coupon.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuecai.modules.activity_coupon.domain.YcActivityCoupon;
import com.yuecai.modules.activity_coupon.domain.dto.YcActivityGiveCouponDto;
import com.yuecai.modules.activity_coupon.mapper.YcActivityCouponMapper;
import com.yuecai.modules.activity_coupon.mapper.YcActivityGiveCouponMapper;
import com.yuecai.modules.yc_coupon.domain.YcCoupon;
import com.yuecai.modules.yc_coupon.service.YcCouponService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dm
 * @since 2021-01-28
 */
@Service
@RequiredArgsConstructor
public class YcActivityCouponService extends ServiceImpl<YcActivityCouponMapper, YcActivityCoupon> implements IService<YcActivityCoupon> {

    private final YcActivityGiveCouponMapper ycActivityGiveCouponMapper;
    private final YcCouponService ycCouponService;

    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<Object> create(YcActivityGiveCouponDto resources) throws Exception {
        Set<Long> couponIds = resources.getCouponIds();
        Set<YcCoupon> ycCouponByType = ycCouponService.findYcCouponByType(7, couponIds);
        HashSet<YcActivityCoupon> ycActivityCoupons = new HashSet<>();
        if (CollectionUtils.isNotEmpty(ycCouponByType)){
            ycActivityGiveCouponMapper.insert(resources);
            Long id = resources.getId();
            for (YcCoupon ycCoupon : ycCouponByType) {
                YcActivityCoupon ycActivityCoupon = new YcActivityCoupon();
                ycActivityCoupon.setConponId(ycCoupon.getId());
                ycActivityCoupon.setActivityId(id);
                ycActivityCoupons.add(ycActivityCoupon);
            }
            saveBatch(ycActivityCoupons);
            return ResponseEntity.ok("新增成功！");
        }
        throw new Exception("优惠券不符合要求！");
    }


    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<Object> updateGive(YcActivityGiveCouponDto resources) throws Exception {
        Set<Long> couponIds = resources.getCouponIds();
        Set<YcCoupon> ycCouponByType = ycCouponService.findYcCouponByType(7, couponIds);
        HashSet<YcActivityCoupon> ycActivityCoupons = new HashSet<>();
        if (CollectionUtils.isNotEmpty(ycCouponByType)){
            Long id = resources.getId();
            remove(Wrappers.<YcActivityCoupon>lambdaQuery().eq(YcActivityCoupon::getActivityId,id));
            ycActivityGiveCouponMapper.updateById(resources);
            for (YcCoupon ycCoupon : ycCouponByType) {
                YcActivityCoupon ycActivityCoupon = new YcActivityCoupon();
                ycActivityCoupon.setConponId(ycCoupon.getId());
                ycActivityCoupon.setActivityId(id);
                ycActivityCoupons.add(ycActivityCoupon);
            }
            saveBatch(ycActivityCoupons);
            return ResponseEntity.ok("新增成功！");
        }
        throw new Exception("优惠券不符合要求！");
    }
}
